package com.bangdream.michelia.presenter;

import android.app.ProgressDialog;

import com.bangdream.michelia.application.AppCurrentUser;
import com.bangdream.michelia.contract.CoursesContract;
import com.bangdream.michelia.contract.TrainPlanContract;
import com.bangdream.michelia.entity.CommentBean;
import com.bangdream.michelia.entity.CoursesTypeBean;
import com.bangdream.michelia.entity.TaskCurriculumBean;
import com.bangdream.michelia.entity.TrainPlanBean;
import com.bangdream.michelia.entity.TrainPlanDetail;
import com.bangdream.michelia.entity.courses.CoursesBean;
import com.bangdream.michelia.model.ICoursesModelImpl;
import com.bangdream.michelia.model.ITrainPlanModelImpl;
import com.bangdream.michelia.presenter.base.BasePresenter;
import com.bangdream.michelia.tool.rejava.BaseObserverEntityNormalHttp;
import com.bangdream.michelia.tool.retrofithttp.TokenManagerDef;
import com.bangdream.michelia.utils.L;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @date on 2018/8/1 20:57
 * @email 909732184@qq.com
 * 培训计划
 */
public class TrainPlanPresenter<V> extends BasePresenter<V> {
    private static final String TAG = "TrainPlanPresenter";
    private TrainPlanContract.ITrainPlanModel model;

    public TrainPlanPresenter() {
        model = new ITrainPlanModelImpl();
    }
    public void getCoursesListData(final int pageNo, int pageSize, ProgressDialog dialog){
        if(getMyView()==null)
            return;
        setPd(dialog);
        getTrainPlanListData(pageNo,pageSize,null,dialog);
    }
    /**
     *
     * @param pageNo
     * @param pageSize
     * @param paramsMap  查询条件
     * @param dialog
     */
    public void getTrainPlanListData(final int pageNo, int pageSize,Map paramsMap, ProgressDialog dialog){
        setPd(dialog);
        if(paramsMap==null){
            paramsMap = new HashMap();
        }
        //删除标记（0正常、1回收站、2、彻底删除）
        paramsMap.put("pageNo",pageNo);
        paramsMap.put("pageSize",pageSize);
        paramsMap.put("userId", AppCurrentUser.getInstance().getUserInfo().getId());
        Map finalParamsMap = paramsMap;
        TokenManagerDef.work(() -> model.getTrainPlanList(finalParamsMap)).compose(TokenManagerDef.newInstance().getTransformer())
                .compose(composeFunction)
                .subscribe(new BaseObserverEntityNormalHttp<List<TrainPlanBean>>(this, dialog) {
                    @Override
                    public void onHandleSuccess(List<TrainPlanBean> data) {
                        if (getMyView()==null)
                            return;
                        getMyView().setTrainPlanList(true,pageNo,data,getDescribe(data));
                    }

                    @Override
                    public void onHandleError(int code, String message) {
                        super.onHandleError(code, message);
                        getMyView().setTrainPlanList(false,pageNo,null,message);
                    }
                });
    }

    public void getTaskCurrilumListData(final int pageNo, int pageSize,Map paramsMap, ProgressDialog dialog){
        setPd(dialog);
        if(paramsMap==null){
            paramsMap = new HashMap();
        }
        //删除标记（0正常、1回收站、2、彻底删除）
        paramsMap.put("pageNo",pageNo);
        paramsMap.put("pageSize",pageSize);
        paramsMap.put("userId", AppCurrentUser.getInstance().getUserInfo().getId());
        Map finalParamsMap = paramsMap;
        TokenManagerDef.work(() -> model.getTaskCurriculumList(finalParamsMap)).compose(TokenManagerDef.newInstance().getTransformer())
                .compose(composeFunction)
                .subscribe(new BaseObserverEntityNormalHttp<List<TaskCurriculumBean>>(this, dialog) {
                    @Override
                    public void onHandleSuccess(List<TaskCurriculumBean> data) {
                        if (getMyView()==null)
                            return;
                        getMyView().setTaskCurriculumList(true,pageNo,data,getDescribe(data));
                    }

                    @Override
                    public void onHandleError(int code, String message) {
                        super.onHandleError(code, message);
                        if (getMyView()==null)
                            return;
                        getMyView().setTaskCurriculumList(false,pageNo,null,message);
                    }
                });
    }

    //获取详情
    public void getTrainPlanDetails( String id, ProgressDialog dialog){
        setPd(dialog);
        if(getMyView()==null)
            return;
        Map paramsMap = new HashMap();
        paramsMap.put("planId",id);
        TokenManagerDef.work(() -> model.getTrainPlanDetails(paramsMap)).compose(TokenManagerDef.newInstance().getTransformer())
                .compose(composeFunction)
                .subscribe(new BaseObserverEntityNormalHttp<TrainPlanDetail>(this,pd){
                    @Override
                    public void onHandleSuccess(TrainPlanDetail bean) {
                        getMyView().setTrainPlanDetails(true,bean);
                    }

                    @Override
                    public void onHandleError(int code, String message) {
                        super.onHandleError(code, message);
                        getMyView().setTrainPlanDetails(false,null);
                    }
                });
    }

    private TrainPlanContract.ITrainPlanView getMyView() {
        return (TrainPlanContract.ITrainPlanView) obtainView();
    }
}
